class Solution:
    def movesToMakeZigzag(self, nums: list) -> int:
        ans = 0
        decrease = 0
        pre = nums[0]
        # 变化为先大后小
        for i in range(1, len(nums)):
            if i % 2 == 0:
                while nums[i] <= pre:
                    pre -= 1
                    decrease += 1
                else:
                    pre = nums[i]
            else:
                if nums[i] >= pre:
                    n = nums[i]
                    while n >= pre:
                        n -= 1
                        decrease += 1
                    pre = n
                else:
                    pre = nums[i]
        ans = decrease
        decrease = 0
        pre = nums[0]
        # 变化为先小后大
        for i in range(1, len(nums)):
            if i % 2 != 0:
                while nums[i] <= pre:
                    pre -= 1
                    decrease += 1
                else:
                    pre = nums[i]
            else:
                if nums[i] >= pre:
                    n = nums[i]
                    while n >= pre:
                        n -= 1
                        decrease += 1
                    pre = n
                else:
                    pre = nums[i]

        return min(decrease, ans)
